/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package madsgtdinterface;

import javax.swing.event.EventListenerList;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import thunderbirdgtd.GTDParent;
import thunderbirdgtd.entities.Action;
import thunderbirdgtd.entities.Project;

/**
 *
 * @author Joel Campos
 */
public class ProjectModel extends AbstractTreeModel{
  protected EventListenerList listeners = new EventListenerList();
  GTDParent root;
  



    public ProjectModel(GTDParent p)
    {
        root = p;
    }


    public Object getRoot()
    { 
       return root;
    }

    public boolean isLeaf(Object node)
    {
        return node instanceof Action;
    }

    public int getChildCount(Object parent)
    {
       
        if (parent instanceof Project){
            Project p = (Project) parent;
            return p.assignedActions.size() + p.assignedProjects.size();
        }
        else
            if (parent instanceof GTDParent){
                return root.nParentProjects;
            }
            else
                return 0;
    }

    public Object getChild(Object parent, int index)
    {
       
        if (parent instanceof Project){
            Project p = (Project) parent;
            if (index+1 <= p.assignedProjects.size())
                return p.assignedProjects.get(index);
            else
                return p.assignedActions.get(index - p.assignedProjects.size());
        }
        else
            if (parent instanceof GTDParent && ((GTDParent) parent).projects.size()>0)
                return ((GTDParent) parent).projects.get(index);
            else
                return null;
    }

    public int getIndexOfChild(Object parent, Object child)
    { 
        if (parent instanceof Project){
            Project p = (Project) parent;
            return p.assignedActions.indexOf(child);
        }
        else
            if (parent instanceof GTDParent)
                return ((GTDParent) parent).projects.indexOf(child);
            else
                return -1;
        
    }

}
